<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/26
 * Time: 16:18
 */

namespace Admin\Controller;
use Admin\Controller\BaseController;
use Common\ORG\Util\UploadFile;

class QuotedController extends BaseController
{
    /**
     * 构造函数
     * ItemController constructor.
     */
    public function __construct()
    {
        //无需进行权限检测的功能
        \Org\Auth\AuthUtil::setNoAuthFuncList([]);
        parent::__construct();
    }

    /**
     * 报价列表
     */
    public function quotedList(){
        $resource = I('get.resource');
        $platform = I('get.platform');
        $put_target = I('get.put_target');
        //分页
        $page = I('get.page', 1);
        $pagesize = 15;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $resource && $params['resource'] = $resource;
        $platform && $params['platform'] = $platform;
        $put_target && $params['put_target'] = $put_target;

        $result = D('Quoted', 'Logic')->searchQuotedList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $quotedList = $result['quotedList'];

        //生成分页
        $url = "/quoted/quotedList?resource={$platform}&platform={$platform}&put_target={$put_target}";
        $page_html = create_page($url, $total, $page, $page_size);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('resource', $resource);
        $this->assign('platform', $platform);
        $this->assign('put_target', $put_target);

        $this->assign('quotedList', $quotedList);

        $this->loadFrame('quotedList');
    }

    /**
     * edit
     */
    public function editQuoted(){
        $id = I('get.id');
        $quotedInfo = [];
        if($id){
            $quotedInfo = D('Quoted','Logic')->getQuotedInfo(['id'=>$id]);
        }

        $this->assign('quotedInfo',$quotedInfo);
        $this->loadFrame('editQuoted');
    }
    /**
     * 上传报价
     * @return string
     * @throws \Exception
     */
    public function ajaxSaveImport(){
        set_time_limit(0);
        ini_set('memory_limit', -1);

        if (!empty($_FILES['import']['name'])) {
            $upload = new UploadFile();
            $upload->maxSize = 93145728;
            $upload->saveRule = time;
            $upload->allowExts = array('xls');
            $upload->savePath = C('STATIC_PATH') . '/attachment/file/quoted/';
            if (!$upload->upload()) {
                return Response(2001,$upload->getErrorMsg());
            }

            //套餐导入数据
            $info = $upload->getUploadFileInfo();

            //解析excel文件
            Vendor('Excel.PHPExcel');
            $file_name = $info[0]['savepath'] . $info[0]['savename'];
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
            $objPHPExcel = $objReader->load($file_name, $encode = 'utf-8');
            $sheet = $objPHPExcel->getSheet(0);
            $highestRow = $sheet->getHighestRow(); // 取得总行数
            $tmp_result = array();
            for ($i = 2; $i <= $highestRow; $i++) {
                //服务项目id
                $tmp_result = [
                        'resource' => $objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue(),
                        'platform' => $objPHPExcel->getActiveSheet()->getCell("B" . $i)->getValue(),
                        'place' => $objPHPExcel->getActiveSheet()->getCell("C" . $i)->getValue(),
                        'put_target' => $objPHPExcel->getActiveSheet()->getCell("D" . $i)->getValue(),
                        'pv' => $objPHPExcel->getActiveSheet()->getCell("E" . $i)->getValue(),
                        'cpm' => $objPHPExcel->getActiveSheet()->getCell("F" . $i)->getValue(),
                        'price' => $objPHPExcel->getActiveSheet()->getCell("G" . $i)->getValue(),
                        'put_time' => \PHPExcel_Shared_Date::ExcelToPHP($objPHPExcel->getActiveSheet()->getCell("H" . $i)->getValue()),
                        'put_day' => $objPHPExcel->getActiveSheet()->getCell("I" . $i)->getValue(),
                        'total_price' => $objPHPExcel->getActiveSheet()->getCell("J" . $i)->getValue()
                ];
                $data = [];
                $data['resource'] = $tmp_result['resource'];
                $data['platform'] = $tmp_result['platform'];
                $data['place'] = $tmp_result['place'];
                $data['put_target'] = $tmp_result['put_target'];

                $quotedInfo = D('Quoted','Logic')->getQuotedInfo($data);
                if(!$quotedInfo){
                    D('Quoted','Logic')->addQuoted($tmp_result);
                }
                unset($tmp_result);
                unset($data);
            }
            //删除文件
            unlink($file_name);
            return Response(999,'请求成功');
        }else{
            return Response(2002,"请选择要上传的文件");
        }
    }

    /**
     * @return string
     */
    public function ajaxDeleteQuoted(){
        $id = I('post.id');
        if(!$id){
           return Response(201,"数据异常");
        }
        D('Quoted','Logic')->deleteQuoted(['id'=>$id]);
    }

    /**
     * ajax save
     */
    public function ajaxSaveQuoted(){
        $id = I('post.id');
        $data = [];
        $id && $data['id'] = $id;
        isset($_POST['resource']) && $data['resource'] = trim($_POST['resource']);
        isset($_POST['platform']) && $data['platform'] = trim($_POST['platform']);
        isset($_POST['place']) && $data['place'] = trim($_POST['place']);
        isset($_POST['put_target']) && $data['put_target'] = trim($_POST['put_target']);
        isset($_POST['pv']) && $data['pv'] = intval(trim($_POST['pv']));
        isset($_POST['cpm']) && $data['cpm'] = intval(trim($_POST['cpm']));
        isset($_POST['put_time']) && $data['put_time'] = strtotime(trim($_POST['put_time']));
        isset($_POST['put_day']) && $data['put_day'] = intval(trim($_POST['put_day']));
        isset($_POST['price']) && $data['price'] = floatval(trim($_POST['price']));
        isset($_POST['total_price']) && $data['total_price'] = floatval(trim($_POST['total_price']));

        D('Quoted','Logic')->saveQuoted($data);
    }
}